from random import random, randrange
from Korean import vowel, consonant
'''
Tips:
    Golbal variable not working on function assignment
    Ref: https://stackoverflow.com/questions/929777/why-does-assigning-to-my-global-variables-not-work-in-python
    
    GUI framwork Considering 
    PyQt https://www.riverbankcomputing.com/static/Docs/PyQt5/introduction.html
    PySimpleGui https://pysimplegui.readthedocs.io/en/latest/
'''
# Track Variable For final Report
countResult = 0
timeCount = 0
ErrorList = {}
countNumber = 5
testList = []
nowTestList = [] #测试序列
mode = 1
key_List = []
value_List = []

def CreateRandomList(countNumer):
    global testList

    vowelRandomList = []
    consonantRandomList = []
    for index in range(0, int(countNumer), 1):
        vowelRandomList.append(randrange(0, len(vowel)))
    for index in range(0, int(countNumer), 1):
        consonantRandomList.append(randrange(0, len(consonant)))
    testList.append(vowelRandomList)
    testList.append(consonantRandomList)

def TestSwitch():
    global mode, nowTestList

    print("What mode would you want test?\n\r")
    mode = int(input("1.Korean vowel, 2.Korean consonants\n\r"))
    if mode == 1:
        nowTestList = testList[0]
    else:
        nowTestList = testList[1]

def randomTest(): 
    global key_List, value_List, nowTestList, countResult
    index = 0
    print("Now Start Korean Vowel and Constants test")
    if mode == 1:
        key_List = list(vowel.keys())
        value_List = list(vowel.values())
    else:
        key_List = list(consonant.keys())
        value_List = list(consonant.values())
    while index < len(nowTestList):
        firstKey = key_List[nowTestList[index]]
        print(firstKey)
        sendIn = GetInput()
        #get second key of dict and compare the value
        secondKey = value_List[nowTestList[index]]
        if sendIn == secondKey :
            countResult += 1
            index += 1
        else:
            print("You are not right.")
            index += 1
            
def GetInput():
    store = input("Input the answer:\n")
    if(type(store) is str):
        return store
    else:
        print("you Input is Error type.\n")
        GetInput()

def printResult():
    print("----- This is test Your Result -------\n")
    print("Totally test is: {0}\n".format(countNumber))
    print("Your result right: {}".format(countResult))
    print("Your Error List is:")
    for k, v in ErrorList.items():
        print("{korean} -> {romaji}\n".format(k,v))
            
def main():
    CreateRandomList(countNumber)
    TestSwitch()
    randomTest()
    printResult()